package com.yxq.mdt.app.modules.bottomsheet;

import android.widget.CheckBox;

import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.yxq.mdt.app.R;
import com.yxq.mdt.app.glide.GlideHelper;
import com.yxq.mdt.app.modules.order.bean.OrderConfirmBean;
import com.yxq.mdt.app.modules.shoppingcart.bean.CartBean;
import com.yxq.mdt.app.utils.StringUtils;

import java.util.List;

/**
 * @author L
 * @time 2019-10-21
 * @describe 优惠券列表
 */
public class CouponListAdapter extends BaseQuickAdapter<OrderConfirmBean.MchListBean.CouponListBean, BaseViewHolder> {

    OrderConfirmBean.MchListBean.CouponListBean couponListBeanChecked;

    public interface CartCheckedInterface {
        void checked(int position);
    }

    private CartCheckedInterface cartCheckedInterface;

    public void setCartCheckedInterface(CartCheckedInterface cartCheckedInterface) {
        this.cartCheckedInterface = cartCheckedInterface;
    }

    public CouponListAdapter(int layoutResId, @Nullable List<OrderConfirmBean.MchListBean.CouponListBean> data, OrderConfirmBean.MchListBean.CouponListBean couponListBeanChecked) {
        super(layoutResId, data);
        this.couponListBeanChecked = couponListBeanChecked;
    }

    @Override
    protected void convert(BaseViewHolder helper, OrderConfirmBean.MchListBean.CouponListBean item) {

        helper.setText(R.id.coupon_tv_desc_price, item.getMin_price_desc());
        helper.setText(R.id.coupon_tv_desc_title, item.getEvent_desc());
        helper.setText(R.id.coupon_tv_price, String.format("%.2f", item.getSub_price()));
        helper.setText(R.id.coupon_tv_desc_time, String.format("%s-%s", StringUtils.formatDate(item.getBegin_time()), StringUtils.formatDate(item.getEnd_time())));
        CheckBox checkBox = helper.getView(R.id.coupon_checkbox);
        checkBox.setOnCheckedChangeListener((compoundButton, b) -> {
            item.setChecked(b);
            if (cartCheckedInterface != null) {
                cartCheckedInterface.checked(getParentPosition(item));
            }
        });
        if (couponListBeanChecked != null && couponListBeanChecked.getUser_coupon_id().equals(item.getUser_coupon_id())) {
            item.setChecked(true);
        } else {
            item.setChecked(false);
        }
        checkBox.setChecked(item.isChecked());

    }

}
